package com.calefk.modules.memory {
	/**
     * Clase de optimizacion , reutiliza instancias ya creadas
     * @author 
     */
    public class Pool {
        
        private var mObjects:Array;
        private var mMaxSize:int;
        private var mFactoryMethod:Function;
        
        public function get sizePool():int { return mObjects.length; }
        
        public function Pool(factoryMethod:Function , maxSize:int = 50) {
            mMaxSize = maxSize;
            mFactoryMethod = factoryMethod;
            mObjects = new Array();
        }
        
        public function alloc():* {
            if (mObjects.length > 0) {
                // reutiliza una instancia
                return mObjects.pop();
            } else {
                // crea una nueva instancia de un objeto
                return mFactoryMethod();
            }
        }
        
        public function free(obj:*):void {
            if (mObjects.length < mMaxSize) {
                mObjects.push(obj);
            }
        }
        
        public function clean():void {
            mObjects.splice(0 , mObjects.length);
            mObjects = null;
            mFactoryMethod = null;
        }

    }

}